#include "stdint.h"
#include "stdbool.h"
#include "api_os.h"
#include "api_event.h"
#include "api_debug.h"
#include "api_hal_spi.h"
#include "api_hal_gpio.h"
#include "api_hal_pm.h"
#include "ssd1306.h"



#define SSD1306_PIN_RST GPIO_PIN6
#define SSD1306_PIN_DC  GPIO_PIN7
#define SSD1306_SPI          SPI2


#define SSD1306_WIDTH   128  // x
#define SSD1306_HEIGHT  64   // y

///////////////////////////////////////////////////////////////////////////

const unsigned char bmp_rocket[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xC0,0xE0,0xE0,0xF0,0xF0,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xF0,0xF8,0xF8,0xFC,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE7,0x83,0x81,0x81,0x81,0xC3,0xFF,0xFF,0xFF,0xFF,0x7F,0x0F,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x1F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x03,0x03,0x03,0x07,0x87,0x87,0x87,0x87,0x87,0x87,0x0F,0x1F,0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x0F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xF0,0xFC,0xFE,0xFF,0xFF,0xFF,0xE7,0xF3,0xF1,0xF0,0xF0,0xF0,0xF8,0xFC,0x38,0x00,0x01,0x73,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1E,0x1F,0x0F,0x07,0x07,0x03,0x0F,0x0F,0x07,0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x07,0x1F,0x1F,0x0F,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};/* (48 X 48 )*/
//列行式



#define AppMain_TASK_STACK_SIZE    (1024 * 2)
#define AppMain_TASK_PRIORITY      0
HANDLE mainTaskHandle  = NULL;


void EventDispatch(API_Event_t* pEvent)
{
    switch(pEvent->id)
    {
        case API_EVENT_ID_POWER_ON:
            break;
        case API_EVENT_ID_NO_SIMCARD:
            break;
        case API_EVENT_ID_NETWORK_REGISTERED_HOME:
        case API_EVENT_ID_NETWORK_REGISTERED_ROAMING:
            break;
        default:
            break;
    }
}



void Init_Interface()
{
    // init spi
	SPI_Config_t spi_cfg =
    {
	    .cs = SPI_CS_0,
        .txMode = SPI_MODE_DIRECT_POLLING,
        .rxMode = SPI_MODE_DIRECT_POLLING,
        .freq = 40000000,//40M here
        .line = SPI_LINE_3,
        .txOnly = false,
        .cpol = 0,
        .cpha = 0,
        .csActiveLow = true,
        .dataBits = SPI_DATA_BITS_8,
        .irqHandler = NULL,
        .irqMask = {0,0,0,0,0}
    };
    SSD1306_Init(SSD1306_WIDTH,SSD1306_HEIGHT,SSD1306_SPI,&spi_cfg,SSD1306_PIN_RST,SSD1306_PIN_DC);
	
}


void SSD1306_Task(void* param)
{
    Init_Interface();

    //show hello world and height 16 bits
    SSD1306_ShowString(8,0,"==hello oled==",16);

    while(1)
    {
        for(int8_t i=0;i<(128-48);++i)
        {
            SSD1306_DrawBMP(i,47+i,2,7,(unsigned char*)bmp_rocket);
            OS_Sleep(10);
        }
        for(int8_t i=(128-48);i>=0;--i)
        {
            SSD1306_DrawBMP(i,47+i,2,7,(unsigned char*)bmp_rocket);
            OS_Sleep(10);
        }
    }
}

void AppMainTask(void *pData)
{
    API_Event_t* event=NULL;
            
    OS_CreateTask(SSD1306_Task ,
        NULL, NULL, AppMain_TASK_STACK_SIZE, AppMain_TASK_PRIORITY+1, 0, 0, "oled Task");
    while(1)
    {
        if(OS_WaitEvent(mainTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER))
        {
            EventDispatch(event);
            OS_Free(event->pParam1);
            OS_Free(event->pParam2);
            OS_Free(event);
        }
    }
}
void oled_spi_Main(void)
{
    mainTaskHandle = OS_CreateTask(AppMainTask ,
        NULL, NULL, AppMain_TASK_STACK_SIZE, AppMain_TASK_PRIORITY, 0, 0, "init Task");
    OS_SetUserMainHandle(&mainTaskHandle);
}

